home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 101-125 / disk_107 / svtools / memlist / memlist.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  4KB  |  159 lines

  1. /***************************************************
  2.   memlist.c
  3.  
  4.   By: Stephen Vermeulen
  5.       3635 Utah Dr. N.W.,
  6.       Calgary, Alberta,
  7.       CANADA, T2N 4A6
  8.  
  9.       (403) 282-7990.
  10.  
  11.   Completely public domain, for what its worth...
  12.  
  13.   A program to dump the systems memory list to the
  14.   display or a file so that you have a snap shot of
  15.   the list.  This program was written to try and
  16.   understand an apparent memory fragmentation
  17.   problem I was having once.
  18.  
  19. Syntax:
  20.  
  21.   memlist
  22.  
  23.     Will dump all the memory lists in the system to stdout.
  24.  
  25.   memlist > file
  26.  
  27.     Will redirect output to the desired file.
  28.  
  29.   memlist n
  30.  
  31.     Will only print out the lists for block n.  N should be 0, 1, ...
  32.     On a 2.5Meg system block 0 will usually be FAST and block 1 will
  33.     be CHIP
  34.  
  35. Output:
  36.  
  37.   Lots of lines like:
  38.  
  39.     memtype start end size running_total
  40.  
  41.   Where:
  42.  
  43.     memtype = 3    CHIP RAM; Header block
  44.     memtype = 5    FAST RAM; Header block
  45.     memtype = 64   Memory fragment within a memory block, the type of the
  46.                    block is given by the previous Header block.
  47.     start          Start address (decimal)
  48.     end            End address (decimal)
  49.     size           Block size (decimal bytes)
  50.     running_total  Running total of number of bytes free in this
  51.                    memory block.  That is total size of all previous
  52.                    fragments since the last header block.
  53.  
  54. ****************************************************/
  55.  
  56. #include <exec/types.h>
  57. #include <exec/nodes.h>
  58. #include <exec/lists.h>
  59. #include <exec/exec.h>
  60. #include <exec/execbase.h>
  61. #include <exec/ports.h>
  62. #include <exec/devices.h>
  63. #include <exec/memory.h>
  64. #include <stdio.h>
  65. #include <functions.h>
  66.  
  67. struct ExecBase *ExecBase;
  68.  
  69. #define MAX_LIST 1000
  70. #define MEM_REGION (1L << 5)
  71. #define MEM_CHUNK  (1L << 6)
  72.  
  73. long att[MAX_LIST];
  74. ULONG start[MAX_LIST], end[MAX_LIST];
  75.  
  76. /*****************************************
  77.   this function scans the system memory
  78.   lists to see what is available and where
  79.   it can be found.
  80. ******************************************/
  81.  
  82. long make_list()
  83. {
  84.   long i;
  85.   struct MemHeader *mem, *loc;
  86.   struct MemChunk *mc;
  87.  
  88.   for (mem = (struct MemHeader *) ExecBase->MemList.lh_Head, i = 0;
  89.                       mem->mh_Node.ln_Succ && (i < MAX_LIST);
  90.                       mem = (struct MemHeader *) mem->mh_Node.ln_Succ)
  91.   {
  92.     att[i]   = (long) mem->mh_Attributes;
  93.     start[i] = (long) mem->mh_Lower;
  94.     end[i]   = (long) mem->mh_Upper;
  95.     ++i;
  96.     for (mc = mem->mh_First; mc && (i < MAX_LIST); mc = mc->mc_Next)
  97.     {
  98.       att[i]   = MEM_CHUNK;
  99.       start[i] = ((ULONG) mc) + 8L;
  100.       end[i]   = ((ULONG) mc) + mc->mc_Bytes + 8L;
  101.       ++i;
  102.     }
  103.   }
  104.   return(i);
  105. }
  106.  
  107. main(argc, argv)
  108. short argc;
  109. char *argv[];
  110. {
  111.   long i, upper, total;
  112.   short blockn, j;
  113.  
  114.   if (argc == 2)
  115.     sscanf(argv[1], "%d", &blockn);
  116.   else
  117.     blockn = -1;
  118.   ExecBase = (struct ExecBase *) OpenLibrary("exec.library", 0L);
  119.   if (!ExecBase) return;
  120.   Forbid();
  121.   upper = make_list();
  122.   Permit();
  123.  
  124.   /***************************************
  125.     Now print it to stdout for the record.
  126.   ****************************************/
  127.  
  128.   if (blockn < 0)
  129.   {
  130.     for (i = 0; i < upper; ++i)
  131.     {
  132.       if (!(att[i] & MEM_CHUNK))
  133.         total = 0;
  134.       else
  135.         total += end[i] - start[i];
  136.       fprintf(stdout, "%ld %ld %ld %ld %ld\n", att[i], start[i], end[i], end[i] - start[i], total);
  137.     }
  138.   }
  139.   else
  140.   {
  141.     j = -1;
  142.     for (i = 0; i < upper; ++i)
  143.     {
  144.       if (!(att[i] & MEM_CHUNK))
  145.       {
  146.         total = 0;
  147.         ++j;
  148.       }
  149.       else
  150.         total += end[i] - start[i];
  151.       if (j == blockn)
  152.         fprintf(stdout, "%ld %ld %ld %ld %ld\n", att[i], start[i], end[i], end[i] - start[i], total);
  153.     }
  154.   }
  155.   CloseLibrary(ExecBase);
  156. }
  157.  
  158.  
  159.